package com.cbts.action.person;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import com.cbts.entity.UserInfo;
import com.cbts.service.info.UserInfoService;
import com.cbts.service.person.PersonalSettingService;
import com.cbts.util.DES3;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
/**
 * @author 邱鸿辉
 *
 */
public class PersonalSettingAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private UserInfoService userInfoService;				//提供员工管理服务
	private PersonalSettingService personalSettingService;	//提供个人设置服务
	private UserInfo userInfo;								//员工
	private String sendResult;								//用于前台传输数据的json结果串
	private String newPassword;								//修改密码时输入的新密码
	private String confirmedPassword;						//修改密码时的确认密码
	private String date;									//接收员工进行邮箱验证时传过来的时间字符串
	private String message;
	private String userid;									//接收员工进行邮箱验证时传过来的经过加密的userid字符串
	
	
	//忘记密码
	public String forgetPWD(){
		
		HttpServletRequest request = ServletActionContext.getRequest();		//声明请求
		sendResult = null;
		
		if (request.getParameter("forgetUserid")!=null) {
			String forgetUserid = request.getParameter("forgetUserid");							//获得参数
			UserInfo userInfo = userInfoService.getUserInfo(forgetUserid);
			if (userInfo!=null) {
				if (userInfo.getEmail()!=null&&"是".equals(userInfo.getEmailValidated())) {
					//调用发邮件的接口
					if (personalSettingService.sendUpdatePWDEmail(userInfo)) {
						sendResult = "修改密码的链接已发送至邮箱:"+userInfo.getEmail()+"\n2天后过期";
					}else{
						sendResult = "邮件发送失败,请检查网络";
					}
					
				}else{
					sendResult = "该用户邮箱没有验证,请与管理员联系！";
				}
			}else {
				sendResult = "账户不存在";
			}
		}
		return "sendlink";
	}
	//通过邮箱链接修改密码
	public String updatePasswordByEmial(){
		
		try{
		 message = personalSettingService.updatePassword(userInfo.getUserid(), newPassword, confirmedPassword);//调用service层的更新密码服务
		
		 if(!"密码修改成功".equals(message))
		 {		
			return ERROR;
		 } 
		
		 return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			message = "密码修改失败";
			return INPUT;
		}
		
	}
	//修改密码
	public String updatePassword(){
	
		try{
		 message = personalSettingService.updatePassword(userInfo, newPassword, confirmedPassword);//调用service层的更新密码服务
		// ActionContext.getContext().put("message", message);
		 if(!"密码修改成功".equals(message))
		 {		
			return ERROR;
		 } 
		// message = null;
		 return SUCCESS;
		}catch(Exception e){
			//addActionError(message);
			/*因为整合了三大框架，现在action交付给了spring管理，
			那么就由原来的多例模式变成了单例模式（即一个action只有一个实例，跟struts1一样，而不再像struts2一样
			是多例模式）。所以当有action级别的错误发生时，假如不清除，那么该错误将一直存在action实例里，直到
			action实例生命周期结束。所以，当第一次发生错误时，使用addActionError()(或者addActionMessage())
			产生action级别的错误，那么下一次再次请求该action时，因为该action级别的错误的存在，将直接返回INPUT*/
			e.printStackTrace();
			message = "密码修改失败";
			/*ActionContext.getContext().put("message", message);
			message = null;*/
			return INPUT;
		}
		
	}
	//获取用户的邮箱
	public String loadEmail(){
		try{
			 userInfo = userInfoService.getUserInfo((String)ActionContext.getContext().getSession().get("userid"));
			/*userInfo = userInfoService.getUserInfo("1");*/
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			return INPUT;
		}
	}
	//修改用户的邮箱
	public String updateEmail(){
		try{
			UserInfo ui = userInfoService.getUserInfo((String)ActionContext.getContext().getSession().get("userid"));
		/*UserInfo ui = userInfoService.getUserInfo("1");*/
		ui.setEmail(userInfo.getEmail());
		personalSettingService.updateEmail(ui);
		message = "邮箱修改成功";
		return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			message = "邮箱修改失败";
			return INPUT;
		}
	}
	//邮箱验证
	public String validateEmail(){
		try{
		/*System.out.println("userid:"+userid);
		System.out.println("email:"+userInfo.getEmail());
		System.out.println("date:"+date);	*/
			int type = 1;
			//修改密码的邮箱验证
			try {
				if("0".equals(ServletActionContext.getRequest().getParameter("type").toString()))
					type = 0; 
			} catch (Exception e) {
				// TODO: handle exception
			}
		userInfo.setUserid(ServletActionContext.getRequest().getParameter("userid").toString());
		userInfo.setUserid(new String( DES3.decryptMode(
				DES3.GetKeyBytes(DES3.getPrivatekey()[DES3.getPrivatekey().length/2])
				,Base64.decode(userInfo.getUserid()))));
		
		message =personalSettingService.validateEmail(userInfo, userInfo.getUserid(), date);

		if(message != null)
		{
			ActionContext.getContext().put("message", message);
			message = null;
		}
		if (!"日期过期失效".equals(message)&&type==0) {
			Map session=ServletActionContext.getContext().getSession();
			session.put("userid", userInfo.getUserid());
			//成功跳转修改密码页面
			return "valipwd";
		}
		if(!"邮箱验证成功".equals(message))
			return ERROR;
		//验证成功
		
		return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			message = "邮箱验证失败";
			return INPUT;
		}
	}
	//发送邮件
	public String sendEmail(){
		try{
			UserInfo ui = userInfoService.getUserInfo((String)ActionContext.getContext().getSession().get("userid"));
			//userInfo = userInfoService.getUserInfo("1");
			if(ui.getEmail()!= null &&!ui.getEmail().trim().equals(userInfo.getEmail().trim())
					||ui.getEmail()==null&&userInfo.getEmail()!=null&&!userInfo.getEmail().trim().equals(""))
			{
				ui.setEmail(userInfo.getEmail().trim());
				ui.setEmailValidated("否");
				userInfoService.updateUserInfo(ui);
			}
			 
			if(ui.getEmail() != null)
			{
				if(personalSettingService.sendEmail(ui))
				{	
					message = "邮件发送成功";
					return SUCCESS;
				}
				else
				{
					message = "邮件发送失败";
					return ERROR;
				}
			}
			else
			{
				message = "您的邮箱为空，无法发送邮箱";
				return ERROR;
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "邮件发送失败";
			return INPUT;
		}
	}
	public String loadUserInfo(){
		try{
			System.err.println(ServletActionContext.getRequest().getSession().getAttribute("userid"));
		userInfo = userInfoService.getUserInfo((String)ActionContext.getContext().getSession().get("userid"));
		//userInfo=userInfoService.getUserInfo("1");
		if(message != null)
		{
			ActionContext.getContext().put("message", message);
			message = null;
		}
		return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			return INPUT;
		}
		
	}
	public String updateUserInfo(){
		try{
			UserInfo ui = userInfoService.getUserInfo((String)ActionContext.getContext().getSession().get("userid"));
			//UserInfo ui = userInfoService.getUserInfo("1");
			ui.setUserName(userInfo.getUserName());
			ui.setGender(userInfo.getGender());
			if(ui.getEmail()!= null &&!ui.getEmail().trim().equals(userInfo.getEmail().trim())
					||ui.getEmail()==null&&userInfo.getEmail()!=null&&!userInfo.getEmail().trim().equals(""))
			{
				ui.setEmail(userInfo.getEmail().trim());
				ui.setEmailValidated("否");
				
			}
			userInfoService.updateUserInfo(ui);
			//ActionContext.getContext().put("message", "修改成功");
			message ="修改成功";
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			//ActionContext.getContext().put("message", "修改失败");
			message = "修改失败";
			return INPUT;
		}
	}
	
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	public PersonalSettingService getPersonalSettingService() {
		return personalSettingService;
	}
	public void setPersonalSettingService(
			PersonalSettingService personalSettingService) {
		this.personalSettingService = personalSettingService;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getConfirmedPassword() {
		//System.out.println("1");
		return confirmedPassword;
	}
	public void setConfirmedPassword(String confirmedPassword) {
		this.confirmedPassword = confirmedPassword;
	}
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public UserInfo getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}
	public String getSendResult() {
		return sendResult;
	}
	public void setSendResult(String sendResult) {
		this.sendResult = sendResult;
	}
	
}
